<?php
	/**
	* 
	*/
	class MachineAction extends Action
	{
		
		public function index(){
			$condition=[];
			if(isset($_REQUEST['search_content'])&&!empty($_REQUEST['search_content'])){
				$condition['os']=array('EQ',$_REQUEST['search_content']);
				$condition['type']=array('LIKE','%'.$_REQUEST['search_content'].'%');
				$condition['owner']=array('EQ',$_REQUEST['search_content']);
				$condition['ownproject']=array('LIKE','%'.$_REQUEST['search_content'].'%');
				$condition['inuse']=array('EQ',$_REQUEST['search_content']);
				$condition['_logic']='OR';

			}

			$size=10;
			// 总条数
			$total_counts=D('Machine')->getCounts($condition);
			//总页数
			$total_pages=ceil($total_counts/$size);
			$current_p=isset($_REQUEST['p'])?$_REQUEST['p']:1;
			$start=($current_p-1)*$size;
			$previous_p=1;
			$previous_disable=true;
			if($current_p>1){
				$previous_p=$current_p-1;
				$previous_disable=false;
			}
			$next_p=$total_pages;
			$next_disable=true;
			if($current_p<$total_pages){
				$next_p=$current_p+1;
				$next_disable=false;
			}			

			$machines=D('Machine')->getMachines($condition,$start,$size);
			$this->assign('next_p',$next_p);
			$this->assign('next_disable',$next_disable);
			$this->assign('previous_p',$previous_p);
			$this->assign('previous_disable',$previous_disable);
			$this->assign('current_p',$current_p);
			$this->assign('total_pages',$total_pages);
			$this->assign('total_counts',$total_counts);
			$this->assign('machines',$machines);
			$this->display('Machine/index');
		}

		public function ajaxDoAdd(){
			$result=array(
				'status'=>0,
				'data'=>'数据不全',
				);

			if(empty($_REQUEST['os']) || empty($_REQUEST['type']) || empty($_REQUEST['owner']) ||empty($_REQUEST['ownproject']) || empty($_REQUEST['inuse'])){
				$this->ajaxReturn($result);
			}

			$data['os']=$_REQUEST['os'];
			$data['type']=$_REQUEST['type'];
			$data['owngroup']='QAT';
			$data['owner']=$_REQUEST['owner'];
			$data['ownproject']=$_REQUEST['ownproject'];
			$data['inuse']=$_REQUEST['inuse'];
			$data['modifytime']=date('y-m-d');

			if(!D('Machine')->addMachine($data)){
				$result['status']=0;
				$result['data']='插入失败';
				$this->ajaxReturn($result);
			}

			$result['status']=1;
			$result['data']='插入成功';
			$this->ajaxReturn($result);
		}

		public function ajaxDoDel(){
			$result=array(
				'status'=>0,
				'data'=>'数据错误',
				);

			if(empty($_REQUEST['id'])){
				$this->ajaxReturn($result);
			}

			$condition['id']=$_REQUEST['id'];

			if(!D('Machine')->delMachine($condition)){
				$result=array(
					'status'=>0,
					'data'=>'删除失败',
				);
				$this->ajaxReturn($result);
			}

			$result=array(
				'status'=>1,
				'data'=>'删除成功',
				);
			$this->ajaxReturn($result);

		}
	}